<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini"></el-button>
        <el-button v-permission="['超级管理员','管理员']" type="primary" icon="el-icon-plus" size="mini" @click="dialogFormVisible = true">添加</el-button>
      </div>
      <div class="table_searchs flex">
        <!-- <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="手册" value="1"></el-option>
              <el-option label="版本" value="2"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </div> -->
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- table -->
    <el-table stripe style="width: 100%" :data="browserTable" :key="key" border fit highlight-current-row ref="tableDataRef">
      <!-- <el-table-column v-if="colData[0].istrue" min-width="40" align="center" prop="id" label="ID"></el-table-column> -->
      <el-table-column v-if="colData[0].istrue" min-width="130" align="center" prop="title" label="新用户手册上传"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="130" align="center" prop="size" label="文件大小"></el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="140" align="center" prop="createtime" label="上传时间">
        <template slot-scope="scope">{{scope.row.createtime| newDate}}</template>
      </el-table-column>
      <el-table-column min-width="150" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="imgShow(scope.row.url)">查看</el-button>
            <el-button size="mini" @click="getDelBackImage(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 接口没分页功能 -->
    
    <!-- 添加 -->
    <el-dialog title="操作系统上传" :visible.sync="dialogFormVisible" @close="addFormclose" :close-on-click-modal="false" :close-on-press-escape="false" width="35%">
      <el-form :model="addForm" :rules="rules" ref="ruleForm">
        <el-form-item label="标题" label-width="100px" prop="title">
          <el-input v-model="addForm.title" class="add_select" placeholder="请输入标题" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="图片上传" label-width="100px" class="snElsx" prop="file">
          <div class="addPut">
            <el-input autocomplete="off" placeholder="请点击上传，上传图片" v-model="addForm.file"></el-input>
            <el-upload class="upload-demo dlePut" style="top:0;right:10px" action :auto-upload="false" :show-file-list="false" :on-change="compileapkAvatarUpload">
              <el-button size="mini">上传</el-button>
            </el-upload>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="mini" round>取 消</el-button>
        <Debounce :time='500' :isDebounce="3"><el-button type="primary" @click.native="getaddBackImage('ruleForm')" size="mini" round>确 定</el-button></Debounce>
      </div>
    </el-dialog>
    <!-- 图片查看 -->
    <el-dialog :visible.sync="imgdialogVisible" width="40%" @close="addFormclose" title="　">
      <div style="">
        <img width="100%" :src="dialogImageUrl" alt="">
      </div>
    </el-dialog>
    <!-- 删除弹窗 -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { backImageList, uploadBackImage, addBackImage, DelBackImage } from "@/api/allocation.js";
import { importManual } from "@/api/handbook.js";
import permission from '@/directive/permission/index.js' // 权限判断指令
import pdf from 'vue-pdf'
import deleteDialog from "@/components/deleteDialog";
export default {
  components: { dynamicTable, pdf, deleteDialog },
  directives: { permission },
  data() {
    return {
      checkboxVal: ["新用户手册上传", "文件大小", "上传时间"],
      key: 1,
      colData: [], //动态控制table
      //归属table
      browserTable: [],
      total: 0,
      searchTable: "",
      select: "", //搜索条件
      dialogFormVisible: false,
      //添加表单
      addForm: {
        title: "",
        file: ""
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        file: [{ required: true, message: "请上传图片", trigger: "blur" }]
      },
      //图片查看
      imgdialogVisible: false,
      dialogImageUrl: '',
      //删除弹窗
      deleteDialogs: {
        show: false,
        deleteText: '',
        deleteId: 0
      }
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
    this.getbackImageList()
  },
  methods: {
    //删除
    getDelBackImage(id) {
      this.deleteDialogs.show = true
      this.deleteDialogs.deleteId = id
    },
    //删除按钮事件
    deleteEvents() {
      let params = { userId: localStorage.getItem("ID"), id: this.deleteDialogs.deleteId }
      DelBackImage(params).then(res => {
        this.$message.success('删除成功')
        this.getbackImageList()
      })
    },
    imgShow(img) {
      this.imgdialogVisible = true
      this.dialogImageUrl = img
      console.log('pdf地址', img)
    },
    //添加
    getaddBackImage(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.userId = localStorage.getItem("ID")
          addBackImage(addForm).then(res => {
            this.$message.success('添加成功')
            this.getbackImageList()
            this.dialogFormVisible = false
          }).catch(err => {
            this.$message.error(err.msg)
          })
        } else {
          return false
        }
      })
    },
    //图片上传
    compileapkAvatarUpload(file) {
      var fd = new FormData()
      fd.append('file', file.raw)
      uploadBackImage(fd).then(res => {
        this.addForm.file = res.data.img_path
      })
    },
    getbackImageList() {
      backImageList({ userId: localStorage.getItem("ID") }).then(res => {
        this.browserTable = [res.data]
      }).catch(err => {
        this.browserTable = []
      })
    },
    addFormclose() {
      this.$nextTick(() => {
        this.addForm = {
          title: "",
          file: ""
        },
          this.imgdialogVisible = false,
          this.dialogImageUrl = ''
      })
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>